//
//  OutputWriter.swift
//  WatermarkCamera
//
//  Created by AxcLogo on 4/7/2024.
//

import UIKit
import Foundation
import AxcBedrock

// 流图片采集，不同于拍照，不会处理和闪光灯之间的同步
// 仅用于采集某一时刻下的输出流图片

extension ImageWriter {
    /// 采集图片
    /// - Parameters:
    ///   - articulation: 清晰度
    ///   - aspectRatio: 画幅比
    ///   - block: 回调
    func collectImage(articulation: VideoBuffer.Articulation,

                      aspectRatio: Eyepiece.AspectRatio,

                      block: @escaping AxcBlock.OneParam<UIImage>) {
        self.articulation = articulation

        self.aspectRatio = aspectRatio

        collectImageBlock = block

        isCollectImage = true
    }
}

// MARK: - [ImageWriter]

class ImageWriter: VideoBuffer {
    // Open

    override func putCVBuffer(_ cvBuffer: CVBuffer) {
        guard isCollectImage else { return }

        guard let cgImage = cvBuffer.axc.cgImage() else {
            isCollectImage = false

            return
        }

        let uiimage = UIImage(cgImage: cgImage)

        collectImageBlock?(uiimage)

        isCollectImage = false
    }

    // Private

    /// 是否开始采集图片
    private var isCollectImage: Bool = false

    /// 采集图片的回调
    private var collectImageBlock: AxcBlock.OneParam<UIImage>?
}
